// rpx转px
export const rpxTopx = rpx => {
  const { windowWidth } = swan.getSystemInfoSync()
  return rpx / 750 * windowWidth
}
// px转rpx
export const pxTorpx = px => {
  const { windowWidth } = swan.getSystemInfoSync()
  return px * 750 / windowWidth
}
// 解析URL字符串
export const parseUrl = str => {
  return decodeURIComponent(str).split('&').reduce((obj, pair) => {
    const [key, value] = pair.split('=')
    if (!key || !value) return obj
    obj[key] = value
    return obj
  }, {})
}
// query字符串化
export const stringify = obj => {
  let str = ''
  for (var Key in obj) {
    str += Key + '=' + obj[Key] + '&'
  }
  return str.substring(0, str.length - 1)
}
// 文字识别换行符号
export const strReturn = str => {
  if (!str) return ''
  return str.replace(/\n/g, '<br/>')
}
// 获取当前页面路径与参数
export const getCurrentPagesPath = () => {
  let pages = getCurrentPages()
  let currPage = pages[pages.length - 1]
  return currPage
}
// 跳转报错页
export const toErrorPage = () => {
  const { route, options } = getCurrentPagesPath()
  const queryStr = encodeURIComponent('?' + stringify(options))
  swan.reLaunch({
    url: `/pages/etwork?path=${route}${queryStr}`
  })
}
